// pages/my/my.js
const app = getApp();
var wxbarcode = require("../../../utils/index");
let login = require('../../../config/login');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    domain: app.api.domain,
    userInfo: null,
    shade: false,
    canIUseGetUserProfile: false,
    hiddenLoading: false,
    showQrBool: false
  },
  showQr() {
    const that = this;
    wx.showLoading({
      title: '加载中...',
    })
    let qrData = app.api.domain + '/scan/regproxy?code=' + that.data.userInfo.referral_code
    // let qrData = app.api.domain+'/scan/regproxy?code=123456'
    setTimeout(() => {
      wxbarcode.qrcode("qrcode", qrData, {
        codeSize: 480,
        color: "#000000",
        bgcolor: "#ffffff",
      });
      wx.hideLoading();
      that.setData({
        showQrBool: true
      })
    }, 500)
  },
  hideQr() {
    const that = this;
    that.setData({
      showQrBool: false
    })
  },
  // 收藏
  toCollection() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/collectionAndFocus/collectionAndFocus?tab=collection',
      })
    })
  },
  toRecord() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      console.log(1111);
      wx.navigateTo({
        url: '/page/detail/pages/viewHistories/viewHistories',
      })
    })
  },
  //赠品券
  toMyGifBox() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: `/page/details/pages/gifBox/gifBox`,
      })
    })
  },
  // 推荐码
  toReferralCode() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: `/page/details/pages/referralCode/referralCode`,
      })
    })
  },

  // 会员赠品券核销
  toPresentVerification() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/details/pages/presentVerification/presentVerification',
      })
    })
  },

  // 关注
  toFocus() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/collectionAndFocus/collectionAndFocus?tab=focus',
      })
    })
  },

  // 收货地址
  toAddresslist() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/addresslist/addresslist',
      })
    })
  },
  //分销商订单
  toDistributor() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/distributor/distributor',
      })
    })
  },
  // 我的订单
  toMyOrder(e) {
    const that = this;
    let where = e.currentTarget.dataset.where;
    let status = e.currentTarget.dataset.status;
    let name = e.currentTarget.dataset.name;
    app.checkUserInfoAuthorization().then(() => {
      if (that.data.userInfo[status]) {
        let userInfo = wx.getStorageSync('userInfo');
        userInfo[name] = that.data.userInfo[name];
        wx.setStorageSync('userInfo', userInfo);
      }

      wx.navigateTo({
        url: `/page/detail/pages/myOrder/myOrder?where=${where}`,
      })
    })
  },

  // 余额
  toMyWallet(e) {
    const that = this;
    let money = e.currentTarget.dataset.money;
    let frozen = e.currentTarget.dataset.frozen;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/myWallet/myWallet',
      })
    })
  },
  // 待评价
  toMyComments() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      if (that.data.userInfo.uncommentCountStatus) {
        let userInfo = wx.getStorageSync('userInfo');
        userInfo.uncomment_count = that.data.userInfo.uncomment_count;
        wx.setStorageSync('userInfo', userInfo);
      }
      wx.navigateTo({
        url: '/page/detail/pages/myComments/myComments?index=0',
      })
    })
  },

  // 系统设置
  toSystem() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/system/system',
      })
    })
  },

  // 退换货
  toReturns() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      if (that.data.userInfo.returnCountStatus) {
        let userInfo = wx.getStorageSync('userInfo');
        userInfo.return_count = that.data.userInfo.return_count;
        wx.setStorageSync('userInfo', userInfo);
      }
      wx.navigateTo({
        url: '/page/detail/pages/returns/returns',
      })
    })
  },

  // 领券中心
  toTickets() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/tickets/tickets',
      })
    })
  },


  // 我的优惠券
  toMyTicket() {
    const that = this;
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/myTicket/myTicket',
      })
    })
  },

  // 关于我们
  toAboutUs() {
    wx.navigateTo({
      url: '/page/detail/pages/aboutUs/aboutUs',
    })
  },


  // 设置
  toSet() {
    app.checkUserInfoAuthorization().then(() => {
      wx.navigateTo({
        url: '/page/detail/pages/set/set',
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    that.setData({
      shade: app.globalData.shade
    })
    that.refresh();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    const that = this;
    that.setData({
      hiddenLoading: false
    })
    that.refresh();
  },

  refresh() {
    const that = this;
    login.checkLogin().then(res => {
      app.getInfo(app.globalData.token, function () {
        let userInfo = app.globalData.userInfo
        userInfo.head_pic = app.globalData.userInfo.head_pic.indexOf('http') >= 0 ? app.globalData.userInfo.head_pic : that.data.domain + app.globalData.userInfo.head_pic;
        that.setData({
          userInfo: userInfo,
        })
      })
      setTimeout(() => {
        wx.stopPullDownRefresh()
      }, 1000)
      that.setData({
        hiddenLoading: true
      })
    }).catch(err => {
      app.appCallBack = (e) => {
        let userInfo = app.globalData.userInfo
        userInfo.head_pic = app.globalData.userInfo.head_pic.indexOf('http') >= 0 ? app.globalData.userInfo.head_pic : that.data.domain + app.globalData.userInfo.head_pic;
        that.setData({
          userInfo: userInfo,
        })
        setTimeout(() => {
          wx.stopPullDownRefresh()
        }, 1000)
        that.setData({
          hiddenLoading: true
        })
      }
    })
  }
})